package com.item.controller;

import com.item.bean.Result;
import com.item.bean.UserName;
import com.item.domain.TUsers;
import com.item.service.PcUserNameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.util.Map;

@RestController
@RequestMapping(value = "login")
public class PcLoginController {

    @Autowired
    PcUserNameService pcUserNameService;

    @PostMapping("/login/user")
    @ResponseBody
    public Result loginUser(@RequestBody TUsers userName, HttpServletResponse response) {
        //获取前端传来的账号
        //UserName userName = new UserName();
        //userName.setTelephone(telephone);
        //userName.setPassword(password);
        String telephone = userName.getTelePhone();
        //获取前端传来的密码
        String password = userName.getPassWord();
        System.out.println("前端传来的账号" + telephone);
        System.out.println("前端传来的密码" + password);
        Result result = new Result();
        //访问服务端进行查询校验
        if (telephone != null && password != null) {
            result = pcUserNameService.loginUser(userName);
            if (result.isFlag()) {
                //到这里就是通过了的意思,保存用户的登录状态,使用cookid来存放用户的信息,谁登录我就存到cookie里面
                Cookie cookie = new Cookie("login_telphone", telephone);
                cookie.setPath("/"); //设置有效的路径
                cookie.setMaxAge(30 * 24 * 60 * 1); //有效期1天
                response.addCookie(cookie); //把cookie 响应到客户端
            }
        }
        return result;
    }

    @RequestMapping(value = "/api/user/findAll")
    public void userTest(@RequestBody(required = false) Object object, HttpServletRequest request) {

        System.out.println(object);
        System.out.println(request.getHeader("token"));


    }

}
